package cn.edu.gsau.info.controller;


import cn.edu.gsau.info.models.*;
import cn.edu.gsau.info.myenum.ModuleIdClass;
import cn.edu.gsau.info.service.*;
import cn.edu.gsau.info.util.UploadUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/*
 * @description: 教材与专著模块
 * @author: liu-maobing
 * @date:  
 * @param: 
 * @return: 
 **/
@RestController
@RequestMapping("/jiaocaizhuanzhu")
@JsonSerialize(using = ToStringSerializer.class)
public class JiaocaizhuanzhuController {

    @Autowired
    IJiaocaizhuanzhuService jiaocaizhuanzhuService;

    @Autowired
    IStatusService statusService;

    @Autowired
    IUserService userService;

    @Autowired
    IShenheService shenheService;

    @Value("${gsau.upload.path}")
    private String uploadfile;

    @Autowired
    IZhengmingcailiaoService zhengmingcailiaoService;
    /*
     * @description:添加一条数据以及修改了证明材料的recoreId
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @PostMapping("/add")
    public ResponseResult add(@RequestBody Jiaocaizhuanzhu jiaocaizhuanzhu){

        User user = new User();
        user.setId(1l);
        user.setUserName("liumaobing");
        boolean save = jiaocaizhuanzhuService.save(jiaocaizhuanzhu);
        for (int i = 0; i < jiaocaizhuanzhu.getNewFileName().length; i++) {
            Zhengmingcailiao zhengmingcailiao = new Zhengmingcailiao();
            zhengmingcailiao.setFileName(jiaocaizhuanzhu.getOldFileName()[i]);
            zhengmingcailiao.setFileFullPath(user.getUserName()+"/"+jiaocaizhuanzhu.getNewFileName()[i]);
            zhengmingcailiao.setModuleId(ModuleIdClass.JIAOCAIYUZHUANZHU_id);
            zhengmingcailiao.setRecoreId(jiaocaizhuanzhu.getId());
            zhengmingcailiaoService.save(zhengmingcailiao);
        }
        System.out.println(jiaocaizhuanzhu);
        return new ResponseResult(save? Code.SAVE_OK:Code.SAVE_ERR,save?"添加成功":"添加失败",jiaocaizhuanzhu);
    }
    /*
     * @description:删除一条记录
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable long id) {
        boolean delete = jiaocaizhuanzhuService.removeById(id);
        return new ResponseResult(delete ? Code.DELETE_OK : Code.DELETE_ERR, delete ? "删除成功" : "删除失败", null);

    }
    /*
     * @description:修改一条记录
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @PutMapping("/update")
    public ResponseResult update(@RequestBody Jiaocaizhuanzhu jiaocaizhuanzhu){
        User user = new User();
        user.setId(1l);
        user.setUserName("liumaobing");
        System.out.println(jiaocaizhuanzhu);
        boolean update = jiaocaizhuanzhuService.updateById(jiaocaizhuanzhu);
        if (jiaocaizhuanzhu.getNewFileName()!=null){
            for (int i = 0; i < jiaocaizhuanzhu.getNewFileName().length; i++) {
                Zhengmingcailiao zhengmingcailiao = new Zhengmingcailiao();
                zhengmingcailiao.setFileName(jiaocaizhuanzhu.getOldFileName()[i]);
                zhengmingcailiao.setFileFullPath(user.getUserName()+"/"+jiaocaizhuanzhu.getNewFileName()[i]);
                zhengmingcailiao.setModuleId(ModuleIdClass.JIAOCAIYUZHUANZHU_id);
                zhengmingcailiao.setRecoreId(jiaocaizhuanzhu.getId());
                zhengmingcailiaoService.save(zhengmingcailiao);
            }
        }

        return new ResponseResult(update?Code.UPDATE_OK:Code.UPDATE_ERR,update?"修改成功":"修改失败",jiaocaizhuanzhu);
    }
    /*
     * @description:查找用户
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @GetMapping("/find")
    public ResponseResult findById(){
            User user = new User();
            user.setId(1l);
            Collection collection = new ArrayList();
            collection.add(user.getId());
            List<Jiaocaizhuanzhu> list = userService.listByIds(collection);
            return new ResponseResult(Code.GET_OK, "查询成功", list);
    }
    /*
     * @description:查找填报Id的老师的所有记录
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @GetMapping("/finduser")
    public ResponseResult findByTianbaoId(){
        User user = new User();
        user.setId(1l);
        List<Jiaocaizhuanzhu> list = jiaocaizhuanzhuService.findByTianbaoId(user.getId());
        return new ResponseResult(Code.GET_OK, "查询成功", list);
    }

    /*
     * @description:查询所有的老师的数据
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @GetMapping("findall")
    public ResponseResult findAll(){
        List<Jiaocaizhuanzhu> list = jiaocaizhuanzhuService.list();
        return new ResponseResult(Code.GET_OK,"查询成功",list);
    }
    /*
     * @description:通过当前节点查询到审核list
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @GetMapping("findbynode")
    public ResponseResult findByNode(){
        String node = "待审核" ;
        List<Jiaocaizhuanzhu> list = jiaocaizhuanzhuService.findByNode(node);
        return new ResponseResult(Code.GET_OK,"查询成功",list);
    }
    /*
     * @description: 查询单条记录
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @GetMapping("findbyrecoreid/{id}")
    public ResponseResult findByRecoreId(@PathVariable long id){
        Collection ids = new ArrayList();
        ids.add(id);
        List<Jiaocaizhuanzhu> list = jiaocaizhuanzhuService.listByIds(ids);
        System.out.println(list);
        return new ResponseResult(Code.GET_OK,"查询成功",list);
    }
    /*
     * @description:上传文件
     * @author: liu-maobing
     * @date:
     * @param: file
     * @return: ResponseResult
     **/
    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile file)  {
        User user = new User();
        user.setUserName("liumaobing");
        Map<String,String > map  = new HashMap<>();
        UploadUtil uploadUtil = new UploadUtil();
        map = uploadUtil.filedUpload(file,user.getUserName(),uploadfile);
        return new ResponseResult(200,"保存成功",map);
    }

    /*
     * @description:根据模块id和记录id查询证明材料
     * @author: liu-maobing
     * @date:
     * @param: 模块id 和 记录id
     * @return:  responseResult
     **/
    @GetMapping("/findbymoduleidandrecoreid/{id}")
    public ResponseResult searchbymoduleidandrecoreid(@PathVariable long id){
        List<Zhengmingcailiao> findbymoduleidandrecoreids = jiaocaizhuanzhuService.findByModuleIdAndRecoreId(ModuleIdClass.JIAOCAIYUZHUANZHU_id,id);
        return new ResponseResult(Code.GET_OK,"查询成功",findbymoduleidandrecoreids);
    }
    /*
     * @description:通过当前状态搜索
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @GetMapping("/searchbystatus/{status}")
    public ResponseResult searchByStatus(@PathVariable String  status){
        User user = new User();
        user.setId(1l);
        List<Jiaocaizhuanzhu> result = jiaocaizhuanzhuService.findByTianbaoIdAndStatus(user.getId(),status);
        return new ResponseResult(Code.GET_OK,"查询成功",result);
    }
    /*
     * @description: 根据关键字搜索
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @GetMapping("/searchbykeyword/{keyword}")
    public ResponseResult searchByKeyWord(@PathVariable String keyword){
        User user = new User();
        user.setId(1l);
        List<Jiaocaizhuanzhu> result = jiaocaizhuanzhuService.findByTianbaoIdAndKeyWord(user.getId(),keyword);
        return new ResponseResult(Code.GET_OK,"查询成功",result);
    }
    /*
     * @description:根据关键字和状态搜索
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @PostMapping("/searchbykeywordandstatus")
    public ResponseResult searchByKeyWordAndStatus(@RequestBody Map search){
        User user = new User();
        user.setId(1l);
        List<Jiaocaizhuanzhu> result = jiaocaizhuanzhuService.findByTianbaoIdAndKeyWordAndStatus(user.getId(),search.get("search_message").toString(),search.get("status").toString());
        return new ResponseResult(Code.GET_OK,"查询成功",result);
    }
    /*
     * @description: 查询证明文件通过模块id和记录id
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @GetMapping("/findfilebyrecoreid/{recoreId}")
    public ResponseResult findFileByRecoreId(@PathVariable long recoreId){

        List<Zhengmingcailiao> list = jiaocaizhuanzhuService.findfileByrecoreIdAndModuleId(recoreId,ModuleIdClass.JIAOCAIYUZHUANZHU_id );
        return new ResponseResult(Code.GET_OK,"查询成功",list);
    }

    /*
     * @description:查询分页
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @PostMapping("/findandpage")
    public ResponseResult findAndPage(@RequestBody Map pageitems){
        Integer page = Integer.valueOf(pageitems.get("page").toString());
        Integer rows = Integer.valueOf(pageitems.get("rows").toString());
        User user = new User();
        user.setId(1l);
        IPage iPage = jiaocaizhuanzhuService.findByPage(user.getId(), page, rows);
        return new ResponseResult(Code.GET_OK,"查询成功",iPage);
    }
    /*
     * @description:根据id删除证明材料
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @DeleteMapping("/deletecailiao/{id}")
    public ResponseResult deleteCailiao(@PathVariable long id){
        boolean remove = jiaocaizhuanzhuService.deleteCailiao(id);
        return new ResponseResult(Code.DELETE_OK,"删除成功");
    }

    @GetMapping("/findallshenhe")
    public ResponseResult findALlShenhe(){
        User user = new User();
        user.setId(1l);
        System.out.println(ModuleIdClass.JIAOCAIYUZHUANZHU_id);
        List<Shenhe> findallshenhe = jiaocaizhuanzhuService.findallshenhe(1, 5);
        return new ResponseResult(Code.GET_OK,"查询成功",findallshenhe);
    }

    /*
     * @description: 添加、修改数据时审核表也添加记录
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @PostMapping("/addshenhe")
    public ResponseResult addShenhe(@RequestBody Shenhe shenhe){
        shenhe.setModuleId(ModuleIdClass.JIAOCAIYUZHUANZHU_id);
        long managerId = jiaocaizhuanzhuService.findManagerId(ModuleIdClass.JIAOCAIYUZHUANZHU_id);
        shenhe.setShenheId(managerId);
        Collection<Long> collection = new ArrayList<>();
        collection.add(managerId);
        List<User> users = userService.listByIds(collection);
        shenhe.setShenHeName(users.get(0).getNickName());
        Jiaocaizhuanzhu jiaocaizhuanzhu = new Jiaocaizhuanzhu();
        jiaocaizhuanzhu.setManager(users.get(0).getNickName());
        jiaocaizhuanzhu.setId(shenhe.getRecoreId());
        jiaocaizhuanzhuService.updateById(jiaocaizhuanzhu);
        boolean save = shenheService.save(shenhe);
        System.out.println(shenhe);
        return new ResponseResult(Code.SAVE_OK,"添加成功");
    }

    /*
     * @description: 审核list通过关键字查询
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @GetMapping("/searchbykeywordandnode/{keyword}")
    public ResponseResult searchByKeywordAndNode(@PathVariable String keyword){
        User user = new User();
        user.setId(1l);
        String node = "待审核";
        List<Jiaocaizhuanzhu> result = jiaocaizhuanzhuService.searchByNode(keyword,node);
        return new ResponseResult(Code.GET_OK,"查询成功",result);
    }

    /*
     * @description:通过recoreId和moduleId查询审核记录
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @GetMapping("/findshenhe/{id}")
    public ResponseResult findShenhe(@PathVariable Long id){
        System.out.println(id);
        List<Shenhe> shenhe = jiaocaizhuanzhuService.findShenhe(ModuleIdClass.JIAOCAIYUZHUANZHU_id, id);
        return new ResponseResult(Code.GET_OK,"查询成功",shenhe);
    }

    /*
     * @description:修改审核表，审核时间和审核结果
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @PutMapping("/tijiaoshenhe/{recoreid}")
    public ResponseResult tijiaoShenhe(@PathVariable Long recoreid,@RequestBody Shenhe shenhe){
        boolean shenhes = jiaocaizhuanzhuService.tijiaoShenhe(shenhe, recoreid);
        return new ResponseResult(Code.UPDATE_OK,"添加成功",shenhe);
    }

    /*
     * @description:审核已通过
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @PutMapping("/updatenodepass/{id}")
    public ResponseResult updateNodePass(@PathVariable Long id){
        Jiaocaizhuanzhu jiaocaizhuanzhu = new Jiaocaizhuanzhu();
        jiaocaizhuanzhu.setId(id);
        jiaocaizhuanzhu.setNode("审核已通过");
        jiaocaizhuanzhu.setStatus("流转完成");
        jiaocaizhuanzhuService.updateById(jiaocaizhuanzhu);
        return new ResponseResult(Code.UPDATE_OK,"修改成功");
    }
    /*
     * @description:审核未通过
     * @author: liu-maobing
     * @date:
     * @param:
     * @return:
     **/
    @PutMapping("/updatenodenopass/{id}")
    public ResponseResult updateNodeNoPass(@PathVariable Long id){
        Jiaocaizhuanzhu jiaocaizhuanzhu = new Jiaocaizhuanzhu();
        jiaocaizhuanzhu.setId(id);
        jiaocaizhuanzhu.setNode("审核未通过");
        jiaocaizhuanzhuService.updateById(jiaocaizhuanzhu);
        return new ResponseResult(Code.UPDATE_OK,"修改成功");
    }
}

